package cj.primariaiquest.integration.endpoints;

import static cj.primariaiquest.model.Department.Alimentation;
import static cj.primariaiquest.model.Department.Fraud;
import static cj.primariaiquest.model.Department.ThievesProtection;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cj.primariaiquest.dao.TicketEntityManager;
import cj.primariaiquest.dao.exceptions.DaoException;
import cj.primariaiquest.model.Ticket;

/**
 * Class used only for test purpose.
 * 
 * @author georgian.parvu
 * 
 */
public class TicketApp {
    private static Logger logger = Logger.getLogger(TicketApp.class);

    /**
     * @param args
     */
    public static void main(String[] args) {

	AbstractApplicationContext context = new ClassPathXmlApplicationContext(
		"/integration/messages-flow.xml");

	TicketEntityManager tem = new TicketEntityManager();
	try {
	    tem.deleteAllTickets();
	} catch (DaoException e) {
	    logger.error("Could not delete all tickets because"
		    + e.getStackTrace());
	}

	TicketGateway ticketGateway = (TicketGateway) context
		.getBean("ticketGateway");

	CanceledTicketGateway canceledTicketGateway = (CanceledTicketGateway) context
		.getBean("canceledTicketGateway");

	Ticket ticket = null;

	ticket = new Ticket(Alimentation, 3, true);
	canceledTicketGateway.placeCanceledTicket(ticket);
	ticket = new Ticket(Fraud, 3, true);
	canceledTicketGateway.placeCanceledTicket(ticket);
	ticket = new Ticket(ThievesProtection, 3, true);
	canceledTicketGateway.placeCanceledTicket(ticket);
	for (int i = 1; i < 6; i++) {
	    ticket = new Ticket(Alimentation, i);
	    ticketGateway.placeTicket(ticket);

	    // ticket = new Ticket(Alimentation, i + 10);
	    // ticketGateway.placeTicket(ticket);
	    // ticket = new Ticket(Alimentation, i + 20);
	    // ticketGateway.placeTicket(ticket);
	    // ticket = new Ticket(Alimentation, i + 30);
	    // ticketGateway.placeTicket(ticket);
	    // ticket = new Ticket(Alimentation, i + 40);
	    // ticketGateway.placeTicket(ticket);
	    // ticket = new Ticket(Alimentation, i + 50);
	    // ticketGateway.placeTicket(ticket);
	    // ticket = new Ticket(Alimentation, i + 60);
	    // ticketGateway.placeTicket(ticket);
	    // ticket = new Ticket(Alimentation, i + 70);
	    // ticketGateway.placeTicket(ticket);
	    ticket = new Ticket(Fraud, i);
	    ticketGateway.placeTicket(ticket);
	    ticket = new Ticket(ThievesProtection, i);
	    ticketGateway.placeTicket(ticket);
	}

	try {
	    TimeUnit.SECONDS.sleep(20);
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}

	try {
	    System.out.println(tem.getAllTickets());
	    logger.info(tem.getAllTickets());
	} catch (DaoException e) {
	    logger.error("Could not get all tickets because"
		    + e.getStackTrace());
	}
    }
}
